Allow PostAgent headers to interpolate event data (#1606)

Andrew Cantino 8 gadi atpakaļ
vecāks
revīzija
cc72f79e0b
2 mainītis faili ar 31 papildinājumiem un 9 dzēšanām
  1. 9 8
      app/models/agents/post_agent.rb
  2. 22 1
      spec/models/agents/post_agent_spec.rb

+ 9 - 8
app/models/agents/post_agent.rb

@@ -122,17 +122,19 @@ module Agents
122 122
 
123 123
     def receive(incoming_events)
124 124
       incoming_events.each do |event|
125
-        outgoing = interpolated(event)['payload'].presence || {}
126
-        if boolify(interpolated['no_merge'])
127
-          handle outgoing, event.payload
128
-        else
129
-          handle outgoing.merge(event.payload), event.payload
125
+        interpolate_with(event) do
126
+          outgoing = interpolated['payload'].presence || {}
127
+          if boolify(interpolated['no_merge'])
128
+            handle outgoing, event.payload, headers(interpolated[:headers])
129
+          else
130
+            handle outgoing.merge(event.payload), event.payload, headers(interpolated[:headers])
131
+          end
130 132
         end
131 133
       end
132 134
     end
133 135
 
134 136
     def check
135
-      handle interpolated['payload'].presence || {}
137
+      handle interpolated['payload'].presence || {}, headers
136 138
     end
137 139
 
138 140
     private
@@ -160,9 +162,8 @@ module Agents
160 162
       }
161 163
     end
162 164
 
163
-    def handle(data, payload = {})
165
+    def handle(data, payload = {}, headers)
164 166
       url = interpolated(payload)[:post_url]
165
-      headers = headers()
166 167
 
167 168
       case method
168 169
       when 'get', 'delete'

+ 22 - 1
spec/models/agents/post_agent_spec.rb

@@ -33,7 +33,7 @@ describe Agents::PostAgent do
33 33
     stub_request(:any, /:/).to_return { |request|
34 34
       method = request.method
35 35
       @requests += 1
36
-      @sent_requests[method] << req = OpenStruct.new(uri: request.uri)
36
+      @sent_requests[method] << req = OpenStruct.new(uri: request.uri, headers: request.headers)
37 37
       case method
38 38
       when :get, :delete
39 39
         req.data = request.uri.query
@@ -137,6 +137,18 @@ describe Agents::PostAgent do
137 137
       expect(uri.path).to eq('/a_variable')
138 138
       expect(uri.query).to eq("existing_param=existing_value")
139 139
     end
140
+
141
+    it "interpolates outgoing headers with the event payload" do
142
+      @checker.options['headers'] = {
143
+        "Foo" => "{{ variable }}"
144
+      }
145
+      @event.payload = {
146
+        'variable' => 'a_variable'
147
+      }
148
+      @checker.receive([@event])
149
+      headers = @sent_requests[:post].first.headers
150
+      expect(headers["Foo"]).to eq("a_variable")
151
+    end
140 152
   end
141 153
 
142 154
   describe "#check" do
@@ -199,6 +211,15 @@ describe Agents::PostAgent do
199 211
       expect(@sent_requests[:post][0].data).to eq('<test>hello</test>')
200 212
     end
201 213
 
214
+    it "interpolates outgoing headers" do
215
+      @checker.options['headers'] = {
216
+        "Foo" => "{% credential aws_key %}"
217
+      }
218
+      @checker.check
219
+      headers = @sent_requests[:post].first.headers
220
+      expect(headers["Foo"]).to eq("2222222222-jane")
221
+    end
222
+
202 223
     describe "emitting events" do
203 224
       context "when emit_events is not set to true" do
204 225
         it "does not emit events" do